package com.jrh.web.user.like;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jrh.biz.user.like.UserLikeReadService;
import com.jrh.biz.user.like.UserLikeWriteService;
import com.jrh.dal.UserProfileDao;
import com.jrh.dal.user.like.UserLikeStyleDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Atlas;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.Showroom;
import com.jrh.domain.entity.UserLike;
import com.jrh.web.member.tools.UserAuthUtil;

/**
 * 用户中心喜欢
 * 
 * @author jasen.hsu.cn@gmail.com 2012-8-7 下午7:21:52
 */
@Controller
@RequestMapping("/user")
public class UserLikeController {

    @Autowired
    private UserLikeWriteService userLikeWriteService;

    @Autowired
    private UserLikeReadService  userLikeReadService;

    @Autowired
    private UserLikeStyleDao     userLikeStyleDao;

    @Autowired
    private UserProfileDao       userProfileDao;

    /**
     * 喜欢首页，默认商品
     * 
     * @return
     */
    @RequestMapping("/me")
    public String likeProduct(UserLikeForm userLikeForm, HttpServletRequest request, HttpServletResponse response,
                              Model model) {
        Long userId = UserAuthUtil.getUserId(request, response);
        Page<Product> page = userLikeReadService.listUserLikeProducts(userId, userLikeForm.getPageIndex(),
                                                                      userLikeForm.getStyle(),
                                                                      userLikeForm.getCategory());
        model.addAttribute("_data_", page);
        model.addAttribute("userlikeAllNum", userLikeReadService.getUserlikeAllNum(userId));
        model.addAttribute("userProfile", userProfileDao.getUserProfileByUserId(userId));
        model.addAttribute("styles", userLikeStyleDao.listLikeProductStyle(userId));
        model.addAttribute("form", userLikeForm);
        return "user/like/product";
    }

    /**
     * 喜欢美图集合
     * 
     * @return
     */
    @RequestMapping("/like/atlas/me")
    public String likeaAtlas(UserLikeForm userLikeForm, HttpServletRequest request, HttpServletResponse response,
                             Model model) {
        Long userId = UserAuthUtil.getUserId(request, response);
        Page<Atlas> page = userLikeReadService.listUserLikeAtlas(userId, userLikeForm.getPageIndex(),
                                                                 userLikeForm.getStyle(), userLikeForm.getCategory());
        model.addAttribute("_data_", page);
        model.addAttribute("userlikeAllNum", userLikeReadService.getUserlikeAllNum(userId));
        model.addAttribute("userProfile", userProfileDao.getUserProfileByUserId(userId));
        model.addAttribute("styles", userLikeStyleDao.listLikeAtlasStyle(userId));
        model.addAttribute("form", userLikeForm);
        return "user/like/atlas";
    }

    /**
     * 喜欢样板间集合
     * 
     * @return
     */
    @RequestMapping("/like/showroom/me")
    public String likeaShowroom(UserLikeForm userLikeForm, HttpServletRequest request, HttpServletResponse response,
                                Model model) {

        Long userId = UserAuthUtil.getUserId(request, response);
        Page<Showroom> page = userLikeReadService.listUserLikeShowroom(userId, userLikeForm.getPageIndex(),
                                                                       userLikeForm.getStyle(),
                                                                       userLikeForm.getCategory());
        model.addAttribute("_data_", page);
        model.addAttribute("userlikeAllNum", userLikeReadService.getUserlikeAllNum(userId));
        model.addAttribute("userProfile", userProfileDao.getUserProfileByUserId(userId));
        model.addAttribute("styles", userLikeStyleDao.listLikeShowroomStyle(userId));
        model.addAttribute("form", userLikeForm);
        return "user/like/showroom";
    }

    /**
     * 喜欢美图集合
     * 
     * @return
     */
    @RequestMapping("/like/brand/me")
    public String likeaBrand(UserLikeForm userLikeForm, HttpServletRequest request, HttpServletResponse response,
                             Model model) {
        Long userId = UserAuthUtil.getUserId(request, response);
        Page<Brand> page = userLikeReadService.listUserLikeBrand(userId, userLikeForm.getPageIndex(),
                                                                 userLikeForm.getStyle(), userLikeForm.getCategory());
        model.addAttribute("_data_", page);
        model.addAttribute("userlikeAllNum", userLikeReadService.getUserlikeAllNum(userId));
        model.addAttribute("userProfile", userProfileDao.getUserProfileByUserId(userId));
        model.addAttribute("styles", userLikeStyleDao.listLikeBrandStyle(userId));
        model.addAttribute("form", userLikeForm);
        return "user/like/brand";
    }

    /**
     * 删除喜欢商品
     * 
     * @return
     */
    @RequestMapping("/unlike/{refType}/{refId}")
    public String unlikeProduct(@PathVariable("refType") int refType, @PathVariable("refId") Long refId,
                                HttpServletRequest request, HttpServletResponse response, Model model) {
        Long userId = UserAuthUtil.getUserId(request, response);
        userLikeWriteService.unlike(refType, userId, refId);
        String returnUrl = null;
        switch (refType) {
            case 0:
                returnUrl = "redirect:/user/me.htm";
                break;
            case UserLike.LIKE_REF_TYPE_PRODUCT:
                returnUrl = "redirect:/user/me.htm";
                break;
            case UserLike.LIKE_REF_TYPE_ATLAS:
                returnUrl = "redirect:/user/like/atlas/me.htm";
                break;
            case UserLike.LIKE_REF_TYPE_SHOWROOM:
                returnUrl = "redirect:/user/like/showroom/me.htm";
                break;
            default:
                returnUrl = "redirect:/user/me.htm";
                break;
        }
        return returnUrl;
    }
}
